package com.atguigu.guli.servicemsm.controller;


import com.atguigu.commonutils.utils.RandomUtil;
import com.atguigu.guli.servicemsm.service.MsmService;
import com.atguigu.servicebase.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/msm")
@CrossOrigin //跨域
public class MsmApiController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping(value = "/send/{phone}")
    public R code(@PathVariable String phone) {
        String code = redisTemplate.opsForValue().get(phone);

        if(!StringUtils.isEmpty(code)) return R.ok();

        code = RandomUtil.getFourBitRandom();

        redisTemplate.opsForValue().set(phone, code,5, TimeUnit.MINUTES);
        log.info(code);

        return R.ok();
    }

}
